package Models;

import DB.DBEmployee;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

public class Employee implements DB.RowInterface
{
	private int employeeNo;
	private String name = "";
	private int supervisorNo;
	private Employee supervisor;
	private String address = "";
	private int zipCode;
	private String city = "";
	private int phoneNo;

	public Employee ()
	{}

	public void importData(ResultSet data) throws SQLException
	{
		this.setEmployeeNo(data.getInt("employeeNo"));
		this.setName(data.getString("name"));
		this.setSupervisorNo(data.getInt("supervisorNo"));
		this.setAddress(data.getString("address"));
		this.setZipCode(data.getInt("zipCode"));
		this.setCity(data.getString("city"));
		this.setPhoneNo(data.getInt("phoneNo"));

		DBEmployee dbe = new DBEmployee();
		Employee supervisorRow = dbe.fetchByNo(getSupervisorNo());
		if(supervisorRow == null) supervisorRow = null;
		this.setSupervisor(supervisorRow);
	}

	public HashMap exportData()
	{
		HashMap data = new HashMap();
		data.put("name", getName());
		data.put("address", getAddress());
		data.put("zipCode", getZipCode());
		data.put("phoneNo", getPhoneNo());
		data.put("supervisorNo", getSupervisorNo());
		return data;
	}

	public void setData(String nameS, String addressS, String zipCodeS, String phoneNoS) throws EmptyValueException, NumberFormatException
	{
		if(!nameS.equals("") && !addressS.equals("") && !zipCodeS.equals("") && !phoneNoS.equals(""))
		{
			this.setName(nameS);

			this.setAddress(addressS);

			int zipCodeTemp = Integer.parseInt(zipCodeS);
			this.setZipCode(zipCodeTemp);

			int phoneNoTemp = Integer.parseInt(phoneNoS);
			this.setPhoneNo(phoneNoTemp);
		}
		else
		{
			throw new EmptyValueException();
		}
	}

	public void refreshData()
	{
		Employee employee = new DBEmployee().fetchByNo(getEmployeeNo());
		this.setName(employee.getName());
		this.setAddress(employee.getAddress());
		this.setZipCode(employee.getZipCode());
		this.setCity(employee.getCity());
		this.setPhoneNo(employee.getPhoneNo());
		this.setSupervisor(employee.getSupervisor());
	}

	public static String[] getSearchColumns()
	{
		return new String[] { "employeeNo", "name", "address", "zipCode", "city", "phoneNo" };
	}

	public String getAddress()
	{
		return address;
	}

	public String getCity()
	{
		return city;
	}

	public int getEmployeeNo()
	{
		return employeeNo;
	}

	public String getName()
	{
		return name;
	}

	public int getPhoneNo()
	{
		return phoneNo;
	}

	public Employee getSupervisor()
	{
		return supervisor;
	}

	public int getSupervisorNo()
	{
		return supervisorNo;
	}

	public int getZipCode()
	{
		return zipCode;
	}

	public void setAddress(String address)
	{
		this.address = address;
	}

	public void setCity(String city)
	{
		this.city = city;
	}

	public void setEmployeeNo(int employeeNo)
	{
		this.employeeNo = employeeNo;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public void setPhoneNo(int phoneNo)
	{
		this.phoneNo = phoneNo;
	}

	public void setSupervisor(Employee supervisor)
	{
		this.supervisor = supervisor;
		if(supervisor != null)
		{
			this.setSupervisorNo(supervisor.getEmployeeNo());
		}
	}

	public void setSupervisorNo(int supervisorNo)
	{
		this.supervisorNo = supervisorNo;
	}

	public void setZipCode(int zipCode)
	{
		this.zipCode = zipCode;
	}
}